package hibernate.web;

import hibernate.fileControler.CheckFileHash;
import hibernate.fileControler.MyException;
import hibernate.fileControler.UploadFiles;
import hibernate.model.Aygit;
import hibernate.model.Buzdolabi;
import hibernate.model.Resimler;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("BuzdolabiBean")
@Scope("session")
public class BuzdolabiBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;

	private Buzdolabi selectedBuzdolabi;
	private List<Buzdolabi> buzdolabiList;
	private List<Resimler> selectedBzdlbResimList;
	private Resimler selectedBzdlbResim;

	private SirketprofilBean firmaBean;
	private ResimlerBean resimBean;
	private ArrayList<UploadedFile> fileList = new ArrayList<UploadedFile>();

	@Override
	@PostConstruct
	public void init() {
	}

	@Override
	public String restart() {
		init();
		return "";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedBuzdolabi);
		init();
		return "list";
	}

	@Override
	public String viewNew() {
		System.out.println("malibu");
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setYeniUrun(true);
		Sirketprofil srf = ((SirketprofilBean) emmyService
				.getBean(new SirketprofilBean())).getCurrentSirketpro();
		Aygit agt = new Aygit(srf, "BUZDOLABI", "", "");

		selectedBuzdolabi = new Buzdolabi();
		selectedBuzdolabi.setAygit(agt);
		selectedBuzdolabi.setSirketprofil(srf);
		agtBean.setSelectedAygitObject(selectedBuzdolabi);
		agtBean.setEdit(true);
		return "";
	}

	@Override
	public String viewDetail() {/*
								 * List<Buzdolabi> valueList = (List<Buzdolabi>)
								 * buzdolabiTable.getValue();
								 * System.out.println(
								 * "table size : "+valueList.size());
								 * Iterator<SimpleRowKey> keys =
								 * buzdolabiTable.getSelection().getKeys();
								 * 
								 * if(keys.hasNext()){ currentBuzdolabi=
								 * valueList.get(keys.next().intValue());
								 * System.
								 * out.println("dolu = "+currentBuzdolabi.
								 * getAygit()); return "detail"; }else{
								 * System.out.println("bo�ta"); return null; }
								 */
		return "detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		AygitBean agtBean = (AygitBean) emmyService.getBean(new AygitBean());
		agtBean.setSelectedAygitObject(selectedBuzdolabi);
		resimBean = (ResimlerBean) emmyService.getBean(new ResimlerBean());
		try {
			if (resimBean.SaveOrUpdate().equals("OK")) {
				fileList = resimBean.getFileList();
				Set<Resimler> resCollec = new HashSet<Resimler>();
				for (int i = 0; i < fileList.size(); i++) {
					Resimler res = new Resimler();
					res.setResimKonum("../productImages/Buzdolabi/"
							+ (CheckFileHash.fileCheck(fileList.get(i)) + UploadFiles.getFileExtension(fileList.get(i).getFileName()) ));
					res.setAygit(selectedBuzdolabi.getAygit());
					resCollec.add(res);
					System.out.println(resCollec.size());
				}
				selectedBuzdolabi.getAygit().setResimlers(resCollec);
				emmyService.saveOrUpdate(selectedBuzdolabi);
				agtBean.deleteSelectedAygit();
				emmyService.clearSession(resimBean);
			}
		} catch (Exception e) {
			System.out.println("Buzdolabı Save error : " + e);
		}
		return "";
	}

	@SuppressWarnings("unchecked")
	public String getDetayliBuzdolabi() {
		buzdolabiList = (List<Buzdolabi>) emmyService.getQuery(createQuery());
		return "list";
	}

	@Override
	public String createQuery() {
		Buzdolabi bzd = selectedBuzdolabi;
		String query = "from Buzdolabi";

		if (bzd == null) {
			return query;
		}
		if (bzd.getSirketprofil().getFirmaid() != 0) {
			if (query.indexOf("where") != -1) {
				query += " and ureticiid=" + bzd.getSirketprofil().getFirmaid()
						+ "";

			} else {
				query += " where ureticiid="
						+ bzd.getSirketprofil().getFirmaid() + "";
			}
		}
		if (bzd.getRenk() != null) {
			if (query.indexOf("where") != -1) {
				query += " and renk like '%" + bzd.getRenk() + "%'";

			} else {
				query += " where renk like '%" + bzd.getRenk() + "%'";
			}
		}
		if (bzd.getTipi() != null) {
			if (query.indexOf("where") != -1) {
				query += " and tipi like '%" + bzd.getTipi() + "%'";

			} else {
				query += " where tipi like '%" + bzd.getTipi() + "%'";
			}
		}
		return query;
	}

	@Override
	public void getSession() {
		// TODO Auto-generated method stub

	}

	public Buzdolabi getSelectedBuzdolabi() {
		return selectedBuzdolabi;
	}

	public void setSelectedBuzdolabi(Buzdolabi selectedBuzdolabi) {
		this.selectedBuzdolabi = selectedBuzdolabi;
	}

	public List<Buzdolabi> getBuzdolabiList() {
		return buzdolabiList;
	}

	public void setBuzdolabiList(List<Buzdolabi> buzdolabiList) {
		this.buzdolabiList = buzdolabiList;
	}

	public List<Resimler> getSelectedBzdlbResimList() {
		return selectedBzdlbResimList;
	}

	public void setSelectedBzdlbResimList(List<Resimler> selectedBzdlbResimList) {
		this.selectedBzdlbResimList = selectedBzdlbResimList;
	}

	public Resimler getSelectedBzdlbResim() {
		return selectedBzdlbResim;
	}

	public void setSelectedBzdlbResim(Resimler selectedBzdlbResim) {
		this.selectedBzdlbResim = selectedBzdlbResim;
	}

	public SirketprofilBean getFirmaBean() {
		return firmaBean;
	}

	public void setFirmaBean(SirketprofilBean firmaBean) {
		this.firmaBean = firmaBean;
	}

	public ResimlerBean getResimBean() {
		return resimBean;
	}

	public void setResimBean(ResimlerBean resimBean) {
		this.resimBean = resimBean;
	}

	public ArrayList<UploadedFile> getFileList() {
		return fileList;
	}

	public void setFileList(ArrayList<UploadedFile> fileList) {
		this.fileList = fileList;
	}
}